package com.cmstop.bzgd.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.graphics.drawable.ColorDrawable;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;

import com.cmstop.bzgd.R;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;

public class SelectCountryPopupWindow extends PopupWindow {

    private LinearLayout viewLayout;
    private CustomFontTextView btn_bazhou, btn_nanjiang, btn_tongjiang, btn_pingchang,btn_enyang;
    private View mMenuView;
    private OnSelectCountryClickListener listener;

    public SelectCountryPopupWindow(Activity context, final OnSelectCountryClickListener listener) {
        super(context);
        this.listener = listener;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_select_country, null);
        viewLayout = (LinearLayout) mMenuView.findViewById(R.id.view_layout);
        btn_bazhou = (CustomFontTextView) mMenuView.findViewById(R.id.txt_bazhou);
        btn_nanjiang = (CustomFontTextView) mMenuView.findViewById(R.id.txt_nanjiang);
        btn_tongjiang = (CustomFontTextView) mMenuView.findViewById(R.id.txt_tongjiang);
        btn_pingchang = (CustomFontTextView) mMenuView.findViewById(R.id.txt_pingchang);
        btn_enyang = (CustomFontTextView) mMenuView.findViewById(R.id.txt_enyang);
        //取消按钮
        viewLayout.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });
        //设置按钮监听
        btn_bazhou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onSelectedCountry(0);
                }
                dismiss();
            }
        });
        btn_enyang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onSelectedCountry(1);
                }
                dismiss();
            }
        });
        btn_nanjiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onSelectedCountry(2);
                }
                dismiss();
            }
        });
        btn_tongjiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onSelectedCountry(3);
                }
                dismiss();
            }
        });
        btn_pingchang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onSelectedCountry(4);
                }
                dismiss();
            }
        });

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x70000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.center_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
//                    if (y < height) {
                        dismiss();
//                    }
                }
                return true;
            }
        });

    }

    public interface OnSelectCountryClickListener {
        void onSelectedCountry(int position);
    }
}
